package com.example.waterdispenserm2.base;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.viewbinding.ViewBinding;

import com.example.waterdispenserm2.util.SPUtils;
import com.example.waterdispenserm2.util.LanguageUtil;

import java.util.ArrayList;


public abstract  class BasicsActivity<T extends ViewBinding> extends Activity implements View.OnClickListener {
    public T binding;


    /** View点击 **/
    public abstract void widgetClick(View v);
    /**
     * [设置监听]
     */
    public abstract void setListener();

    /**
     * [初始化控件]
     */
    public abstract void initView();


    @SuppressLint("WrongConstant")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = getViewBinding();
        setContentView(binding.getRoot());
        requestPermissions(99);
        setListener();
        initView();
        //注册
        Window window = getWindow();
        if (window == null) return;
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        window.getDecorView()
                .setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        WindowManager.LayoutParams lp = window.getAttributes();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
        }
        window.setAttributes(lp);

    }

    @Override
    public void onClick(View v) {
        widgetClick(v);
    }

    protected abstract T getViewBinding();

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void requestPermissions(int requestCode) {
        try {
            if (Build.VERSION.SDK_INT >= 23) {
                ArrayList<String> requestPerssionArr = new ArrayList<>();
                int hasCamrea = checkSelfPermission(Manifest.permission.CAMERA);
                if (hasCamrea != PackageManager.PERMISSION_GRANTED) {
                    requestPerssionArr.add(Manifest.permission.CAMERA);
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    boolean haveInstallPermission = getPackageManager().canRequestPackageInstalls();
                    if (!haveInstallPermission) {//没有权限
                        Uri packageURI = Uri.parse("package:" + getPackageName());

//注意这个是8.0新API

                        Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);

                        startActivityForResult(intent, 2);
                    }
                }

                int hasSdcardRead = checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
                if (hasSdcardRead != PackageManager.PERMISSION_GRANTED) {
                    requestPerssionArr.add(Manifest.permission.READ_EXTERNAL_STORAGE);
                }

                int RECORD_AUDIO = checkSelfPermission(Manifest.permission.RECORD_AUDIO);
                if (RECORD_AUDIO != PackageManager.PERMISSION_GRANTED) {
                    requestPerssionArr.add(Manifest.permission.RECORD_AUDIO);
                }

                int hasSdcard = checkSelfPermission(Manifest.permission.READ_PHONE_STATE);
                if (hasSdcard != PackageManager.PERMISSION_GRANTED) {
                    requestPerssionArr.add(Manifest.permission.READ_PHONE_STATE);
                }

                int hasSdcardWrite = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
                if (hasSdcardWrite != PackageManager.PERMISSION_GRANTED) {
                    requestPerssionArr.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
                }
                int hasLOGS= checkSelfPermission(Manifest.permission.READ_LOGS);
                if (hasLOGS != PackageManager.PERMISSION_GRANTED) {
                    requestPerssionArr.add(Manifest.permission.READ_LOGS);
                }
                int FOREGROUND_SERVICE= checkSelfPermission(Manifest.permission.FOREGROUND_SERVICE);
                if (FOREGROUND_SERVICE != PackageManager.PERMISSION_GRANTED) {
                    requestPerssionArr.add(Manifest.permission.FOREGROUND_SERVICE);
                }

                // 是否应该显示权限请求
                if (requestPerssionArr.size() >= 1) {
                    String[] requestArray = new String[requestPerssionArr.size()];
                    for (int i = 0; i < requestArray.length; i++) {
                        requestArray[i] = requestPerssionArr.get(i);
                    }
                    requestPermissions(requestArray, requestCode);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }






    @Override
    protected void attachBaseContext(Context newBase) {
        String language = SPUtils.getString(SPUtils.KEY_LANGUAGE);
        Log.d("Language---get", language+"===");
        super.attachBaseContext(LanguageUtil.attachBaseContext(newBase, language));
    }
}
